package ray.mgocc.title;

import ray.mgocc.Score;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.Properties;

public abstract class RuleTitle extends WeekTitle{
    abstract public String getRule();
    public int getPlayTimeThreshold(){ return 3;}
    public int getPriority(){ return 17; }
    public void init(){
        super.init();
        addRoundRequirement(50,getRule());
        add(new RuleRequirement(getRule()));
    }
    public static List<Title> getList(){
        Properties match = new Properties();
        match.setProperty("Deathmatch","FIGHTING FISH");
        match.setProperty("Team Sneaking","CHAMELEON");
        match.setProperty("Stealth Deathmatch","KOMODO DRAGON");
        match.setProperty("Solo Capture","ARCTIC SKUA");
        match.setProperty("Team Deathmatch","KILLER WHALE");
        match.setProperty("Capture Mission","KEROTAN");
        match.setProperty("Base Mission","ELEPHANT");
        match.setProperty("Bomb Mission","CUCKOO");
        match.setProperty("Race Mission","HOG");
        match.setProperty("Rescue Mission","GA-KO");
        match.setProperty("Sneaking Mission","SNAKE");

        List<Title> list = new ArrayList<Title>();
        Title title;
        for(Map.Entry entry:match.entrySet()){
            final String rule = (String)entry.getKey();
            final String name = (String)entry.getValue();
            title = new RuleTitle(){
                    public String getRule(){return rule;}
                    public String getName(){return name;}
                };
            list.add(title);
        }
        return list;
    }
    class RuleRequirement extends Requirement{
        private String rule;
        RuleRequirement(String rule){
            this.rule = rule;
        }
        public String getName(){ return "rule("+getRule()+")";}
        public String getRule(){
            return this.rule;
        }
        public int getRound(){
            return getValue(getRule()+".ROUND");
        }
        public int getThreshold(){
            // FIXME
            int rule = getRound();
            int max = 0;
            for(Score sample:getRecord()){
                if(!sample.isWeekly() ||
                   !sample.isRule() ||
                   getRule().equals(sample.getName())){
                    continue;
                }
                int sampleRound = getValue(sample.getName()+".ROUND");
                if(max <= sampleRound){
                    max = sampleRound;
                }
            }
            return max + 1;
        }
        public double getRatio(){
            return ((double)getRound())/getThreshold();
        }
        public String toString(){
            return super.toString()+"("+ getThreshold()+"<="+getRound()+"R"+")";
        }
    }
}
